//Get Data
$(document).ready(function(){
    var name=localStorage.getItem("name");
    const Url='http://124.71.207.55:8081/getLatelyPostByNumber/20';
        $.ajax({
            url: Url,
            type: 'POST',
            success:function(result){
                for(var i=0;i<result.length;i++){
                    var html="";
                    var thisData=result[i];
                    //console.log(thisData);
                    (function(thisData){
                        $.ajax({
                            url: 'http://124.71.207.55:8081/getUserByName/'+thisData.name,
                            type: 'POST',
                            success: function(data){
                                
                                $.ajax({
                                    url: 'http://124.71.207.55:8081/checkUserPostFavor/'+name+'/'+thisData.id,
                                    type: 'POST',
                                    success:function(liked){
                                        //console.log(thisData.id + 'checked '+liked);
                                        if(liked==1){
                                            var svg = 'bi-hand-thumbs-up-fill';
                                        }else{
                                            var svg = 'bi-hand-thumbs-up';
                                        }
                                        var otherphoto = data.photopath;
                                        if(otherphoto==null){
                                            otherphoto = '/photos/default.jpg';  // 默认图片 绝对路径
                                        }       
                                        //console.log(thisData.favor);
                                        if(thisData.favor>0){
                                            var like = '<i class="bi '+svg+'" id="'+thisData.id+'">'+thisData.favor+'</i>';
                                        }else{
                                            var like = '<i class="bi '+svg+'" id="'+thisData.id+'"></i>';
                                        }        
                                        html = '<div class="block">'+
                                        '    <img src="'+otherphoto+'" class="icon rounded-circle othericon" alt="'+thisData.name+'">'+
                                        '    <div class="card">'+
                                        '        <div class="card-body">'+
                                        '            <p class="card-text">'+thisData.majority+'</p>'+
                                        '            <img src="'+thisData.photoPath+'" class="foods">'+
                                        '        </div>'+
                                        '        <div class="card-footer text-muted">'+
                                        '        发布时间：'+thisData.timestring+
                                        '            <div class="review">'+ like+
                                        '            </div>'+
                                        '        </div>'+
                                        '    </div>'+
                                        '</div>';
                                        $("#communitymain").append(html);
                                    }
                                });
        
                            }
                        }) 

                    })(thisData)

                }
                
                
            }
        })
});

$(document).on('click','.bi-hand-thumbs-up',function(){
    var name=localStorage.getItem("name");
    $(this).addClass("bi-hand-thumbs-up-fill");
    $(this).removeClass("bi-hand-thumbs-up");
    var get_text = $(this).text();
    if(get_text==""){
        $(this).html("1");
    }else{
        $(this).html(parseInt(get_text)+1);
    }
    $.ajax({
        url: 'http://124.71.207.55:8081/addFavorByNameAndId/'+name+'/'+this.id+'/1',
        type: 'POST',
        
    })
});

$(document).on('click','.bi-hand-thumbs-up-fill',function(){
    var name=localStorage.getItem("name");
    $(this).addClass("bi-hand-thumbs-up");
    $(this).removeClass("bi-hand-thumbs-up-fill");
    var get_text = $(this).text();
    if(get_text=="1"){
        $(this).html("");
    }else{
        $(this).html(parseInt(get_text)-1);
    }
    $.ajax({
        url: 'http://124.71.207.55:8081/addFavorByNameAndId/'+name+'/'+this.id+'/-1',
        type: 'POST',
        
    })
});

$(document).on('click','.othericon',function(){
    var othername = $(this).attr("alt");
    location.href="others.html?othername="+othername;
})